﻿
namespace Red.Launcher.ViewModels.Workspaces.Database.Sql
{
    public partial class Select
    {
        /// <summary>
        /// A Database table
        /// </summary>
        public class Table : IAlias, ISql, ISqlDeclaration
        {
            #region Public Properties

            /// <summary>
            /// The database this table can be found in
            /// </summary>
            public string Database => $"[{Settings.DatabaseName}].[dbo]";

            /// <summary>
            /// The name of the table as it is defined in the database
            /// </summary>
            public string Name { get; set; }

            /// <summary>
            /// A name to reference this table elsewhere in the sql
            /// </summary>
            public string Alias { get; set; }

            /// <summary>
            /// This is the SQL that is used when referencing the table in a WHERE clause or similar
            /// </summary>
            public string Sql => Alias;

            /// <summary>
            /// This is the SQL that is used when referencing the table in a FROM clause or similar
            /// </summary>
            public string SqlDeclaration => $"{Database}.[{Name}]{this.GetAliasDeclaration()}";

            #endregion Public Properties

        }

        #region Public Methods

        /// <summary>
        /// Utility function to reduce boilerplate bloat
        /// </summary>
        public static Table CreateTable( string name, string alias = null )
        {
            return new Table()
            {
                Name = name,
                Alias = alias
            };
        }

        #endregion Public Methods
    }
}
